package com.zf.zffencheng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zf.zffencheng.dao.NtDeviceDao;
import com.zf.zffencheng.exception.ResultException;
import com.zf.zffencheng.model.dto.AllDeviceDTO;
import com.zf.zffencheng.model.pojo.*;
import com.zf.zffencheng.model.vo.DeviceVo;
import com.zf.zffencheng.service.*;
import com.zf.zffencheng.strategy.DeviceFactory;
import com.zf.zffencheng.strategy.DeviceStrategy;
import com.zf.zffencheng.strategy.HardwarePartnerFactory;
import com.zf.zffencheng.strategy.HardwarePartnerStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * ClassName: NtDeviceService <br/>
 * Description:  <br/>
 * date: 2021/5/31 10:54<br/>
 *
 * @author william <br/>
 */
@Service
public class NtDeviceServiceImpl extends ServiceImpl<NtDeviceDao, NtAllDevice> implements NtDeviceService {
    @Autowired
    private HardwarePartnerService hardwarePartnerService;
    @Autowired
    private DonationPlaceService donationPlaceService;
    @Autowired
    private TenantService tenantService;
    @Autowired
    private TenantPalaceService tenantPalaceService;
    @Autowired
    private TenantPalaceRegionService tenantPalaceRegionService;
    /**
     * 创建设备
     *
     * @param allDeviceDTO
     * @return
     */
    @Override
    @Transactional
    public DeviceVo create(AllDeviceDTO allDeviceDTO) {
        DeviceVo deviceVo = new DeviceVo();
        //寺院
        Integer tenantId = allDeviceDTO.getTenantId();
        Tenant tenant = tenantService.getById(tenantId);
        deviceVo.setTenantName(tenant.getTenantName());
        //大殿
        Integer palaceId = allDeviceDTO.getPalaceId();
        TenantPalace tenantPalace = tenantPalaceService.getById(palaceId);
        deviceVo.setPalaceName(tenantPalace.getPalaceName());

        //地区
        Integer regionId = allDeviceDTO.getRegionId();
        PalaceRegion palaceRegion = tenantPalaceRegionService.getById(regionId);
        deviceVo.setRegionName(palaceRegion.getRegionName());

        deviceVo.setCode(allDeviceDTO.getCode());
        deviceVo.setLabel(allDeviceDTO.getLabel());
        deviceVo.setMac(allDeviceDTO.getMac());

        Integer deviceType = allDeviceDTO.getDeviceType();
        DeviceStrategy deviceStrategy = DeviceFactory.get(deviceType);
        NtAllDevice ntAllDevice = deviceStrategy.save(allDeviceDTO);
        HardwarePartner hardwarePartner = hardwarePartnerService.getById(allDeviceDTO.getApiId());
        DonationPlace donationPlace = null;
        if(null != hardwarePartner){
            if(deviceType == 41){
                donationPlace = donationPlaceService.selectByRegionIdSortLngAndLat(regionId);
                if(donationPlace == null){
                    throw new ResultException(500, "该区域已满");
                }
                //列
                deviceVo.setLng(donationPlace.getLongitude());
                //行
                deviceVo.setLnt(donationPlace.getLatitude());
            }
            //版本
            Integer version = hardwarePartner.getVersion();
            ntAllDevice.setEquipmentVersion(version);
            HardwarePartnerStrategy hardwarePartnerStrategy = HardwarePartnerFactory.get(version);
            NtAllDevice save = hardwarePartnerStrategy.save(ntAllDevice, deviceVo);
            save(save);
            Integer id = save.getId();
            if(deviceType == 41){
                donationPlace.setDeviceId(id);
                //修改位置
                donationPlaceService.updateById(donationPlace);
            }

        }
        return deviceVo;
    }

    /**
     * 查询
     *
     * @param deviceSn
     * @param apiId
     * @return
     */
    @Override
    public NtAllDevice getDeviceSnAndApiId(String deviceSn, Integer apiId) {
        QueryWrapper<NtAllDevice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ApiID", apiId);
        queryWrapper.eq("DeviceSn", deviceSn);
        return getOne(queryWrapper, false);
    }
}
